package com.zjx.shop.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjx.shop.common.utils.PageParams;
import com.zjx.shop.product.entity.AttrEntity;
import com.zjx.shop.product.entity.VO.AttrGroupWithAttrsVo;
import com.zjx.shop.product.service.AttrService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjx.shop.common.utils.PageUtils;
import com.zjx.shop.product.dao.AttrGroupDao;
import com.zjx.shop.product.entity.AttrGroupEntity;
import com.zjx.shop.product.service.AttrGroupService;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(PageParams params) {
        IPage<AttrGroupEntity> page = this.page(
                new Page<>(params.getPage(),params.getSize()),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page.getCurrent(),page.getSize(),(long) page.getRecords().size(),page.getRecords());
    }

    @Override
    public PageUtils queryPage(PageParams params, Long categoryId) {
        if (categoryId == 0){
            IPage<AttrGroupEntity> page = this.page(
                    new Page<>(params.getPage(),params.getSize()),
                    new QueryWrapper<AttrGroupEntity>()
            );
            return new PageUtils(page.getCurrent(),page.getSize(),(long) page.getRecords().size(),page.getRecords());
        }else {
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("catelog_id",categoryId);
            if (params.getKey() != null && !Objects.equals(params.getKey(), "")){
                wrapper.like("attr_group_name", params.getKey()).or().eq("attr_group_id", params.getKey());
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Page<>(params.getPage(),params.getSize()),
                    wrapper
            );
            return new PageUtils(page.getCurrent(),page.getSize(),(long) page.getRecords().size(),page.getRecords());
        }
    }

    /**
     *根据分类id查出所有的分组以及这些分组里面的属性
     *@param:[catelogId]
     *@return:java.util.List<com.xmh.gulimall.product.vo.AttrGroupWithAttrsVo>
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(item -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item, attrsVo);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(item.getAttrGroupId());
            attrsVo.setAttrs(relationAttr);
            return attrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

}